package com.jld.managerialsystemforcovid19.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jld.managerialsystemforcovid19.dto.MenuDto;
import com.jld.managerialsystemforcovid19.entity.Menu;
import com.jld.managerialsystemforcovid19.service.MenuService;
import com.jld.managerialsystemforcovid19.utils.TreeNode;
import com.jld.managerialsystemforcovid19.vo.DataView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 贾老丁
 * @description
 * @Date 2022/11/25 17:10
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping("/toMenu")
    public String toMenu() {
        return "menu/menu";
    }

    /**
     * 加载所有菜单
     *
     * @param menuDto
     * @return
     */
    @RequestMapping("/loadAllMenu")
    @ResponseBody
    public DataView loadAllMenu(MenuDto menuDto) {
        IPage<Menu> page = new Page<>(menuDto.getPage(), menuDto.getLimit());
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(menuDto.getTitle()), Menu::getTitle, menuDto.getTitle());
        lambdaQueryWrapper.orderByAsc(Menu::getOrdernum);
        menuService.page(page, lambdaQueryWrapper);
        return new DataView(page.getTotal(), page.getRecords());
    }

    /**
     * 加载下拉菜单数据 初始化下拉数
     */
    @RequestMapping("/loadMenuManagerLeftTreeJson")
    @ResponseBody
    public DataView loadMenuManagerLeftTreeJson() {
        List<Menu> list = menuService.list();
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Menu menu : list) {
            boolean open = menu.getOpen() == 1 ? true : false;
            treeNodes.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), open));
        }
        return new DataView(treeNodes);
    }

    /**
     * 赋值最大的排序码+1
     * 条件查询：倒序排序，取了一条数据+1
     */
    @RequestMapping("/loadMenuMaxOrderNum")
    @ResponseBody
    public Map<String, Object> loadMenuMaxOrderNum() {
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.orderByDesc("ordernum");
        Page<Menu> menuPage = new Page<>();
        List<Menu> menus = menuService.page(menuPage, menuQueryWrapper).getRecords();
        map.put("value", menus.get(0).getOrdernum() + 1);
        return map;
    }

    /**
     * 新增菜单
     */
    @RequestMapping("/addMenu")
    @ResponseBody
    public DataView addMenu(Menu menu) {
        DataView dataView = new DataView();
        menu.setType("menu");
        boolean save = menuService.save(menu);
        if (!save) {
            dataView.setCode(100);
            dataView.setMsg("数据插入失败");
            return dataView;
        }
        dataView.setCode(200);
        dataView.setMsg("菜单新增成功");
        return dataView;
    }

    /**
     * 更新菜单
     */
    @RequestMapping("/updateMenu")
    @ResponseBody
    public DataView updateMenu(Menu menu) {
        DataView dataView = new DataView();
        menuService.updateById(menu);
        dataView.setCode(200);
        dataView.setMsg("菜单修改成功");
        return dataView;
    }

    /**
     * 删除菜单,先判断有无父节点
     */
    @RequestMapping("/checkMenuHasChildrenNode")
    @ResponseBody
    public Map<String, Object> checkMenuHashChildrenNode(Menu menu) {
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", menu.getId());
        List<Menu> list = menuService.list(queryWrapper);
        if (list.size() > 0) {
            map.put("value", true);
        } else {
            map.put("value", false);
        }
        return map;
    }

    /**
     * 进行删除
     */
    @RequestMapping("/deleteMenu")
    @ResponseBody
    public DataView deleteMenu(Menu menu){
        menuService.removeById(menu.getId());
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("菜单成功删除");
        return dataView;
    }
}
